<?php

class WidgetsController extends BaseAdminController {

	public function actionView($id) {
		$this->render('view', array(
			'model' => $this->loadModel($id, 'Widgets'),
		));
	}

	public function actionCreate() {
		$model = new Widgets;
		$widget_name = Yii::app()->request->getParam('name');
		$this->performAjaxValidation($model, 'widgets-form');

		if (isset($_POST['Widgets'])) {
			$model->attributes = $_POST['Widgets'];
			$model->created_by 	= Yii::app()->user->id;
			$model->modified_by = Yii::app()->user->id;
			$model->created = date('Y-m-d H:i:s');
			$model->modified = date('Y-m-d H:i:s');
			
			$widgetClass = new $_POST['widget'];
			$model->params = $widgetClass->getParams($_POST);
			if ($model->save()) {
				if (Yii::app()->request->isAjaxRequest)
					Yii::app()->end();
				else
					$this->redirect(array('view', 'id' => $model->id));
			}
		}
		$this->render('create', array( 'model' => $model, 'widget_name'=>$widget_name));
	}
	public function actionSelect()
	{
		$widgets = Yii::app()->params['widgets'];
		$this->render('select',array('widgets'=>$widgets));
	}
	public function actionUpdate($id) {
		$model = $this->loadModel($id, 'Widgets');
			
		$this->performAjaxValidation($model, 'widgets-form');

		if (isset($_POST['Widgets'])) {
			$model->attributes = $_POST['Widgets'];
			$model->modified_by = Yii::app()->user->id;
			$model->modified = date('Y-m-d H:i:s');
			
			$widgetClass = new $_POST['widget'];
			$model->params = $widgetClass->getParams($_POST);
			if ($model->save()) {
				$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->render('update', array(
				'model' => $model,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->request->isPostRequest) {
			$this->loadModel($id, 'Widgets')->delete();

			if (!Yii::app()->request->isAjaxRequest)
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}
	public function actionDelMulti()
	{
		if(Yii::app()->request->isPostRequest){
			$arr_Id = Yii::app()->request->getParam('data');
			if(strpos($arr_Id,':')!==false){
				$arrid = explode(':', $arr_Id);
			}else{
				$arrid[]=$arr_Id;
			}
				
				foreach ($arrid as $id){
					if(is_numeric($id)){
						$model = $this->loadModel($id, 'Widgets');
						$model->delete();
					}
				}
		}else 
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}
	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('Widgets');
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin() {
		$dir = Yii::getPathOfAlias('application').'/helpers/js';
		$assets_path = Yii::app()->getAssetManager()->publish($dir);
		$cs = Yii::app()->getClientScript();
		$cs->registerScriptFile("{$assets_path}/form.js");
		
		$model = new Widgets('search');
		$model->unsetAttributes();

		if (isset($_GET['Widgets']))
			$model->attributes = $_GET['Widgets'];

		$this->render('admin', array(
			'model' => $model,
		));
	}

}